﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace BO
{
    public class PermissaoBO
    {
        #region Contrutores

        public PermissaoBO() 
        {
           
        }

        #endregion

        #region Leitores
        public static DropDownList Listar(DropDownList ddl)
        {
            try
            {
                var bco = new ModeloContainer();
                var objs = (from a in bco.Permissaos
                            orderby a.id ascending
                            select new
                            {
                                Código = a.id,
                                Descrição = a.Pagina.nome
                            }).ToList();
                ddl.DataSource = objs;
                ddl.DataTextField = "Descrição";
                ddl.DataValueField = "Código";
                ddl.DataBind();
                ddl.Items.Insert(0, "- -");
                return ddl;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        public static List<Permissao> Listar(int vID, int vTipoUsuarioID, int vPaginaID, string vLer, string vInserir, string vAlterar, string vExcluir)
        {
            try
            {
                ModeloContainer bco = new ModeloContainer();

                List<Permissao> objs = (from a in bco.Permissaos where a.id == a.id orderby a.Pagina.ordem ascending select a).ToList();

                if (vID != 0)
                {
                    objs = (from a in objs where a.id == vID select a).ToList();
                }

                if (vTipoUsuarioID != 0)
                {
                    objs = (from a in objs where a.tipoUsuarioID == vTipoUsuarioID select a).ToList();
                }

                if (vPaginaID != 0)
                {
                    objs = (from a in objs where a.paginaID == vPaginaID select a).ToList();
                }

                if (!string.IsNullOrEmpty(vLer))
                {
                    objs = (from a in objs where a.ler == vLer select a).ToList();
                }

                if (!string.IsNullOrEmpty(vInserir))
                {
                    objs = (from a in objs where a.inserir == vInserir select a).ToList();
                }
                if (!string.IsNullOrEmpty(vAlterar))
                {
                    objs = (from a in objs where a.alterar == vAlterar select a).ToList();
                }
                if (!string.IsNullOrEmpty(vExcluir))
                {
                    objs = (from a in objs where a.excluir == vExcluir select a).ToList();
                }

                return objs;
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public static Permissao Selecionar(int vID)
        {
            try
            {
                ModeloContainer bco = new ModeloContainer();

                Permissao obj = bco.Permissaos.FirstOrDefault(x => x.id == vID);
                if (obj == null)
                    throw new Exception("Permissao não localizado");
                else
                    return obj;
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        public static bool Verificar(int? vTipoUsuarioID, string vPagina)
        {
            try
            {
                ModeloContainer bco = new ModeloContainer();

                Permissao obj = bco.Permissaos.FirstOrDefault(x => x.id == vTipoUsuarioID && x.Pagina.link.ToLower() == vPagina.ToLower());

                if (obj == null)
                    return false;
                else
                    return true;
            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion

        #region Modificadores

        public static Permissao Salvar(Permissao obj, int userID)
        {
            try
            {
                var bco = new ModeloContainer();

                if (obj.id == 0)
                {
                    bco.Permissaos.AddObject(obj);
                    bco.SaveChanges();
                }
                else
                {
                    Permissao objNovo = bco.Permissaos.FirstOrDefault(g => g.id == obj.id);

                    objNovo.tipoUsuarioID = obj.tipoUsuarioID;
                    objNovo.paginaID = obj.paginaID;
                    objNovo.ler = obj.ler;
                    objNovo.alterar = obj.alterar;
                    objNovo.inserir = obj.inserir;
                    objNovo.excluir = obj.excluir;

                    bco.SaveChanges();
                }
                return obj;
            }

            catch (Exception)
            {
                throw;
            }
        }
        public static void Excluir(int id, int userID)
        {
            try
            {
                var bco = new ModeloContainer();

                Permissao obj = bco.Permissaos.FirstOrDefault(g => g.id == id);

                if (obj != null)
                {
                    bco.Permissaos.DeleteObject(obj);
                    bco.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower().Contains("constraint"))
                    throw new Exception("Impossivel realizar exclusão, pois há registros relacionados a este Permissao!");
                else
                    throw new Exception("Deletar - " + ex.Message.ToString());
            }
        }
        #endregion
    }
}
